package o;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class bma extends bmf {
    private blz ˋ;
    private SQLiteDatabase ˏ = getWritableDatabase();

    public bma(Context context, blz o_blz) {
        super(context, o_blz.ˊ(), null, o_blz.ˋ());
        this.ˋ = o_blz;
    }

    protected void ˊ(SQLiteDatabase sQLiteDatabase, int i) {
        bmg.ˊ.ॱ("DbHelper", "initDb:" + this.ˋ.ˊ());
        try {
            this.ˏ = sQLiteDatabase;
            sQLiteDatabase.beginTransaction();
            ˋ();
            if (i != -1) {
                sQLiteDatabase.setVersion(i);
            }
            sQLiteDatabase.setTransactionSuccessful();
        } catch (Exception e) {
            bmg.ˊ.ˏ("DbHelper", " initTables error. ");
        } finally {
            sQLiteDatabase.endTransaction();
        }
    }

    /* JADX WARNING: inconsistent code. */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public boolean ॱ(java.lang.String r10) throws android.database.SQLException {
        /*
        r9 = this;
        r2 = 1;
        r0 = 0;
        r1 = r9.ʼ(r10);
        if (r1 != 0) goto L_0x000e;
    L_0x0008:
        r0 = new android.database.SQLException;
        r0.<init>();
        throw r0;
    L_0x000e:
        if (r10 != 0) goto L_0x0011;
    L_0x0010:
        return r0;
    L_0x0011:
        r1 = 0;
        r3 = "select count(1) as c from sqlite_master where type ='table' and name = ?";
        r4 = r9.ˏ;	 Catch:{ SQLException -> 0x0038, all -> 0x004c }
        r5 = 1;
        r5 = new java.lang.String[r5];	 Catch:{ SQLException -> 0x0038, all -> 0x004c }
        r6 = 0;
        r7 = r10.trim();	 Catch:{ SQLException -> 0x0038, all -> 0x004c }
        r5[r6] = r7;	 Catch:{ SQLException -> 0x0038, all -> 0x004c }
        r1 = r4.rawQuery(r3, r5);	 Catch:{ SQLException -> 0x0038, all -> 0x004c }
        r3 = r1.moveToNext();	 Catch:{ SQLException -> 0x004e, all -> 0x004c }
        if (r3 == 0) goto L_0x0032;
    L_0x002a:
        r3 = 0;
        r3 = r1.getInt(r3);	 Catch:{ SQLException -> 0x004e, all -> 0x004c }
        if (r3 <= 0) goto L_0x0032;
    L_0x0031:
        r0 = r2;
    L_0x0032:
        if (r1 == 0) goto L_0x0010;
    L_0x0034:
        r1.close();
        goto L_0x0010;
    L_0x0038:
        r0 = move-exception;
        r0 = r1;
    L_0x003a:
        r1 = new android.database.SQLException;	 Catch:{ all -> 0x0042 }
        r2 = "isTableExist SQLException";
        r1.<init>(r2);	 Catch:{ all -> 0x0042 }
        throw r1;	 Catch:{ all -> 0x0042 }
    L_0x0042:
        r1 = move-exception;
        r8 = r1;
        r1 = r0;
        r0 = r8;
    L_0x0046:
        if (r1 == 0) goto L_0x004b;
    L_0x0048:
        r1.close();
    L_0x004b:
        throw r0;
    L_0x004c:
        r0 = move-exception;
        goto L_0x0046;
    L_0x004e:
        r0 = move-exception;
        r0 = r1;
        goto L_0x003a;
        */
        throw new UnsupportedOperationException("Method not decompiled: o.bma.ॱ(java.lang.String):boolean");
    }

    public void ˋ(String str) throws SQLException {
        if (ʼ(str)) {
            try {
                this.ˏ.execSQL(" ALTER TABLE " + str + " RENAME TO _temp_" + str);
                return;
            } catch (SQLException e) {
                bmg.ˊ.ˏ("DbHelper", "alter table error!");
                throw new SQLException("alter table error!");
            }
        }
        throw new SQLException();
    }

    public void ˎ(String str) throws SQLException {
        ˏ("_temp_" + str);
    }

    public void ˏ(String str) throws SQLException {
        if (ʼ(str)) {
            ˊ(" DROP TABLE " + str);
            return;
        }
        throw new SQLException();
    }

    public void ˊ(String str) throws SQLException {
        try {
            this.ˏ.execSQL(str);
        } catch (SQLException e) {
            bmg.ˊ.ˏ("DbHelper", " executeSQL error ");
            throw new SQLException("DbHelper executeSQL error ");
        }
    }

    /* JADX WARNING: inconsistent code. */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public java.lang.String[] ʽ(java.lang.String r6) throws android.database.SQLException {
        /*
        r5 = this;
        r0 = 0;
        r1 = r5.ʼ(r6);
        if (r1 != 0) goto L_0x000d;
    L_0x0007:
        r0 = new android.database.SQLException;
        r0.<init>();
        throw r0;
    L_0x000d:
        r1 = new java.lang.StringBuilder;
        r1.<init>();
        r2 = " select * from ";
        r1 = r1.append(r2);
        r1 = r1.append(r6);
        r2 = " order by _id asc LIMIT 1";
        r1 = r1.append(r2);
        r1 = r1.toString();
        r2 = r5.ˏ;	 Catch:{ SQLException -> 0x003a, all -> 0x0056 }
        r3 = 0;
        r0 = r2.rawQuery(r1, r3);	 Catch:{ SQLException -> 0x003a, all -> 0x0056 }
        r0.moveToNext();	 Catch:{ SQLException -> 0x003a }
        r1 = r0.getColumnNames();	 Catch:{ SQLException -> 0x003a }
        if (r0 == 0) goto L_0x0039;
    L_0x0036:
        r0.close();
    L_0x0039:
        return r1;
    L_0x003a:
        r1 = move-exception;
        r1 = o.bmg.ˊ;	 Catch:{ all -> 0x004c }
        r2 = "DbHelper";
        r3 = "DbHelper getColumnNames error [] ";
        r1.ˏ(r2, r3);	 Catch:{ all -> 0x004c }
        r1 = new android.database.SQLException;	 Catch:{ all -> 0x004c }
        r2 = "getColumnNames error []";
        r1.<init>(r2);	 Catch:{ all -> 0x004c }
        throw r1;	 Catch:{ all -> 0x004c }
    L_0x004c:
        r1 = move-exception;
        r4 = r1;
        r1 = r0;
        r0 = r4;
    L_0x0050:
        if (r1 == 0) goto L_0x0055;
    L_0x0052:
        r1.close();
    L_0x0055:
        throw r0;
    L_0x0056:
        r1 = move-exception;
        r4 = r1;
        r1 = r0;
        r0 = r4;
        goto L_0x0050;
        */
        throw new UnsupportedOperationException("Method not decompiled: o.bma.ʽ(java.lang.String):java.lang.String[]");
    }

    public void ˋ() throws SQLException {
        bmg.ˊ.ˊ("DbHelper", " initTables begin ");
        ˊ();
        ˏ();
        bmg.ˊ.ˊ("DbHelper", " initTables end ");
    }

    private void ˏ() {
        List<String> ˏ = this.ˋ.ˏ();
        if (ˏ != null) {
            for (String str : ˏ) {
                if (TextUtils.isEmpty(str)) {
                    bmg.ˊ.ˏ("DbHelper", "error tableName");
                } else if (ॱ(str)) {
                    bmg.ˊ.ॱ("DbHelper", "DbHelper drop unused table " + str);
                    ˏ(str);
                }
            }
        }
    }

    private void ˊ() throws SQLException {
        for (Class ॱ : this.ˋ.ॱ()) {
            blw ॱ2 = bmd.ॱ(ॱ);
            if (ॱ2 != null) {
                String ˊ = ॱ2.ˊ();
                if (ॱ(ˊ)) {
                    ˋ(ˊ);
                    bmg.ˊ.ˊ("DbHelper", "DbHelpertableName exist moidfy table " + ˊ + " successfully.");
                    try {
                        ॱ(ॱ2);
                        ᐝ(ˊ);
                        bmg.ˊ.ॱ("DbHelper", "DbHelper insert data to table " + ˊ + " successfully.");
                        ˎ(ˊ);
                        bmg.ˊ.ॱ("DbHelper", "DbHelperdrop table _temp_" + ˊ + " successfully.");
                    } catch (SQLException e) {
                        throw new SQLException("initTablesEx SQLException");
                    }
                }
                bmg.ˊ.ˊ("DbHelper", "DbHelper create table " + ˊ);
                try {
                    ॱ(ॱ2);
                } catch (SQLException e2) {
                    throw new SQLException("initTablesEx SQLException");
                }
            }
        }
    }

    public void ॱ(blw o_blw) {
        ˊ(o_blw.ॱ());
    }

    private void ᐝ(String str) throws SQLException {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(" INSERT INTO ");
        stringBuilder.append(str);
        stringBuilder.append(" SELECT ");
        try {
            try {
                String ˋ = ˋ(ʽ(str), ʽ("_temp_" + str));
                if (ˋ != null) {
                    stringBuilder.append(ˋ);
                    stringBuilder.append(" FROM ");
                    stringBuilder.append("_temp_").append(str);
                    try {
                        ˊ(stringBuilder.toString());
                        return;
                    } catch (SQLException e) {
                        throw new SQLException("DbHelper insertData mDbHelper.executeSQL error");
                    }
                }
                throw new NullPointerException("DbHelper insertData sInsertColumns is null. [tableName=" + str + "]");
            } catch (SQLException e2) {
                throw new SQLException("DbHelper insertData mDbHelper.getColumnNames error ");
            }
        } catch (SQLException e3) {
            throw new SQLException("insertData SQLException");
        }
    }

    private String ˋ(String[] strArr, String[] strArr2) {
        StringBuilder stringBuilder = new StringBuilder();
        ArrayList arrayList = new ArrayList();
        if (strArr2 != null) {
            List asList = Arrays.asList(strArr2);
        } else {
            Object obj = arrayList;
        }
        if (strArr == null || strArr.length <= 0 || strArr2 == null) {
            return null;
        }
        for (int i = 0; i < strArr.length; i++) {
            String str = strArr[i];
            if (asList.contains(str)) {
                stringBuilder.append(str);
            } else {
                stringBuilder.append("\"\"");
            }
            if (i != strArr.length - 1) {
                stringBuilder.append(',');
            }
        }
        return stringBuilder.toString();
    }
}
